﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;

namespace BLL
{
    public class HocSinhBUO : IBUO<tbl_HocSinh>
    {
        CSDLQLDiemHSC3DataContext db = new CSDLQLDiemHSC3DataContext();

        public bool Insert(tbl_HocSinh obj)
        {
            throw new NotImplementedException();
        }

        public bool InsertStudent(tbl_HocSinh obj)
        {
            try
            {
                db.tbl_HocSinhs.InsertOnSubmit(obj);
                db.SubmitChanges();
                return true;
            }
            catch (System.Exception ex)
            {
                return false;
            }

        }

        public bool Delete(string id)
        {
            throw new NotImplementedException();
        }

        public bool Delete(tbl_HocSinh obj)
        {
            throw new NotImplementedException();
        }

        public List<tbl_HocSinh> GetAll()
        {
            throw new NotImplementedException();
        }

        public tbl_HocSinh GetSingle(string id)
        {
            throw new NotImplementedException();
        }
        public bool Update(string id, tbl_HocSinh obj)
        {
            throw new NotImplementedException();
        }
        public bool UpdateStudent(string id, tbl_HocSinh obj)
        {
            tbl_HocSinh hocsinh = db.tbl_HocSinhs.Where(hs => hs.id_HocSinh.Trim() == id.Trim()).FirstOrDefault();
            hocsinh.HoTenHS = obj.HoTenHS;
            hocsinh.GioiTinh = obj.GioiTinh;
            hocsinh.DiaChi = obj.DiaChi;
            hocsinh.NgaySinh = obj.NgaySinh;
            hocsinh.SDT = obj.SDT;
            hocsinh.id_Lop = obj.id_Lop;
            db.SubmitChanges();
            return true;
        }

        public bool UpdateClass(string id, tbl_Lop obj)
        {
            tbl_Lop objclass = db.tbl_Lops.Where(lop => lop.id_Lop.Trim() == id.Trim()).FirstOrDefault();
            objclass.TenLop = obj.TenLop;

            db.SubmitChanges();
            return true;
        }

        public IQueryable showallstudent()
        {
            var liststudent = db.tbl_HocSinhs.Join(
                db.tbl_Lops,
                hs => hs.id_Lop,
                lop => lop.id_Lop,
                (hs, lop) => new
                {
                    id_HocSinh = hs.id_HocSinh,
                    HoTenHS = hs.HoTenHS,
                    NgaySinh = hs.NgaySinh,
                    DiaChi = hs.DiaChi,
                    GioiTinh = hs.GioiTinh,
                    SDT = hs.SDT,
                    TenLop = lop.TenLop
                }
                );
            return liststudent;
        }

        public IQueryable searchbyname(String name)
        {
            var liststudent = db.tbl_HocSinhs.Join(
                db.tbl_Lops,
                hs => hs.id_Lop,
                lop => lop.id_Lop,
                (hs, lop) => new
                {
                    id_HocSinh = hs.id_HocSinh,
                    HoTenHS = hs.HoTenHS,
                    NgaySinh = hs.NgaySinh,
                    DiaChi = hs.DiaChi,
                    GioiTinh = hs.GioiTinh,
                    SDT = hs.SDT,
                    TenLop = lop.TenLop
                }).Where(namehs => namehs.HoTenHS.Contains(name.Trim()));
            return liststudent;
        }

        public IQueryable searchbyclass(String classname)
        {
            var liststudent = db.tbl_HocSinhs.Join(
                db.tbl_Lops,
                hs => hs.id_Lop,
                lop => lop.id_Lop,
                (hs, lop) => new
                {
                    id_HocSinh = hs.id_HocSinh,
                    HoTenHS = hs.HoTenHS,
                    NgaySinh = hs.NgaySinh,
                    DiaChi = hs.DiaChi,
                    GioiTinh = hs.GioiTinh,
                    SDT = hs.SDT,
                    TenLop = lop.TenLop
                }).Where(namelop => namelop.TenLop == classname);
            return liststudent;
        }

        public List<string> getclassname()
        {
            return db.tbl_Lops.Select(namelop => namelop.TenLop).ToList();
        }

        public bool delstudent(String id_hs)
        {
            try
            {
                tbl_HocSinh hs = db.tbl_HocSinhs.Where(idhs => idhs.id_HocSinh == id_hs).FirstOrDefault();
                db.tbl_HocSinhs.DeleteOnSubmit(hs);
                db.SubmitChanges();
                return true;
            }
            catch (System.Exception ex)
            {
                return false;
            }
        }

        public tbl_HocSinh getstudentbyidhs(String idhs)
        {
            return db.tbl_HocSinhs.Where(hs => hs.id_HocSinh == idhs).FirstOrDefault();
        }

        public tbl_Lop getstudentbyidlop(String idlop)
        {
            return db.tbl_Lops.Where(lop => lop.id_Lop == idlop).FirstOrDefault();
        }

        public tbl_Lop getclassbyclassname(String classname)
        {
            return db.tbl_Lops.Where(lop => lop.TenLop.Trim() == classname.Trim()).FirstOrDefault();
        }
    }
}
